package me.zhengjie.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;

/**
 * String工具类
 */
public final class StringUtil {

	public static final String EMPTY = "";

	/**
	 * @param target 需要被判断的字符串
	 * @return 如果@{code target}是null或者是空串则返回true ，反之则返回false
	 */
	public static boolean isNullOrEmpty(String target) {
		return target == null || EMPTY.equals(target.trim());
	}

	public static boolean isNullStr(String target) {
		return target == null || "null".equalsIgnoreCase(target.trim());
	}

	public static boolean isNullOrEmptyAll(String... targets) {
		return Arrays.stream(targets).allMatch(StringUtil::isNullOrEmpty);
	}

	public static boolean isNullOrEmptyAny(String... targets) {
		return Arrays.stream(targets).anyMatch(StringUtil::isNullOrEmpty);
	}

	public static boolean isEmpty(String target) {
		return target != null && EMPTY.equals(target.trim());
	}

	public static String valueOf(String text, String defaultValue) {
		return text == null ? defaultValue : text;
	}

	public static String valueOf(String text) {
		return valueOf(text, "");
	}

	//字符串转为特定数字类型
	@SuppressWarnings("unchecked")
	public static <T> T convert(String value, Class<T> tClass) {
		if (tClass.equals(String.class)) {
			return (T) value;
		}

		if (tClass.equals(Integer.class) || tClass.equals(int.class)) {
			return (T) IntegerUtil.valueOf(value, 0);
		}

		if (tClass.equals(Long.class) || tClass.equals(long.class)) {
			return (T) LongUtil.valueOf(value, 0L);
		}

		if (tClass.equals(Float.class) || tClass.equals(float.class)) {
			return (T) FloatUtil.valueOf(value, 0.0f);
		}

		if (tClass.equals(Double.class) || tClass.equals(double.class)) {
			return (T) DoubleUtil.valueOf(value, 0.0);
		}

		if (tClass.equals(Short.class) || tClass.equals(short.class)) {
			return (T) ShortUtil.valueOf(value, (short) 0);
		}

		if (tClass.equals(Byte.class)) {
			return (T) ByteUtil.valueOf(value, (byte) 0);
		}

		return JsonUtil.json2Obj(value, tClass);
	}

	public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) {
		return String.join(delimiter, elements);
	}

	public static String emptyToNull(String str) {
		if (isNullOrEmpty(str)) {
			return null;
		}
		return str;
	}

	/**
	 * 将double格式化为指定小数位的String，不足小数位用0补全 如果 @{@code v}为null则返回空串
	 * 
	 * @param v     需要格式化的数字
	 * @param scale 小数点后保留几位
	 * @return
	 */
	public static String roundByScale(BigDecimal v, int scale) {
		if (v == null) {
			return StringUtil.EMPTY;
		}
		if (scale < 0) {
			throw new IllegalArgumentException("The   scale   must   be   a   positive   integer   or   zero");
		}
		if (scale == 0) {
			return new DecimalFormat("0").format(v);
		}
		String formatStr = "0.";
		for (int i = 0; i < scale; i++) {
			formatStr = formatStr + "0";
		}
		return new DecimalFormat(formatStr).format(v);
	}

//    public static void main(String[] args) {
//		System.out.println(convert("123", int.class));
//		System.out.println(convert("123", Integer.class));
//		
//		System.out.println(convert("123", float.class));
//		System.out.println(convert("123", Float.class));
//		
//		System.out.println(convert(null, String.class));
//	}

}
